import re
from functools import wraps
from flask import session, redirect, url_for, jsonify, render_template
#校验用户是否登录
def wapper(func):
    @wraps(func)
    def inner(*args,**kwargs):
        if not session.get('user'):
            return redirect(url_for('admin.login'))
        return func(*args,**kwargs)
    return inner
def success_api(msg: str = "成功"):
    """ 成功响应 默认值“成功” """
    return jsonify(success=True, msg=msg)
def fail_api(msg: str = "失败"):
    """ 失败响应 默认值“失败” """
    return jsonify(success=False, msg=msg)
def table_api(msg: str = "", count=0, data=None, limit=10):
    """ 动态表格渲染响应 """
    res = {
        'msg': msg,
        'code': 0,
        'data': data,
        'count': count,
        'limit': limit
    }
    return jsonify(res)
#删掉内容中的html标签
def nohtml(content):
    return(re.compile(r'<[^>]+>',re.S).sub('',content))
#错误处理
def init_error_views(app):
    @app.errorhandler(403)
    def page_not_found(e):
        return render_template('admin/errors/403.html'), 403
    @app.errorhandler(404)
    def page_not_found(e):
        return render_template('admin/errors/404.html'), 404
    @app.errorhandler(500)
    def internal_server_error(e):
        return render_template('admin/errors/500.html'), 500